<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();

$return['follow_xuanxiang']=array (
  1 => '已关注',
  0 => '未关注',
);

if($_RQ['op']=='list'){
	//搜索
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>2);//type为2表示公众号
	if($_RQ['wxgid']){
		$params['wxgid']=intval($_RQ['wxgid']);
		$return['params']['wxgid']=$_RQ['wxgid'];
	}
	if($_RQ['nickname']){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	
	$fields=array('id','wxgid','mid','avatar','follow','nickname','updatetime','tagid_list');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	//exi($params,'error');
	$list=pdo_getslice('core_fans',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),array('username','nickname'));
			$item['mnickname']=$member['nickname']?:'无';
			if(!$item['tagid_list']){
				continue;
			}
			$tags=trim($item['tagid_list'],';');
			$tags=explode(';',$tags);
			$temp='';
			foreach($tags as $tag){
				$temp.=pdo_getcolumn('core_fansgroup',array('uniacid'=>$_SESSION['uniacid'],'wxgid'=>$item['wxgid'],'tag_id'=>$tag),'title');
				$temp.='|';
			}
			$temp=trim($temp,'|');
			$item['tags']=$temp;
			
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['wechats']=pdo_getall('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'粉丝管理列表'),
			'1'=>array('op'=>'get','title'=>'同步粉丝信息'),
		),
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
			array('name'=>'nickname','title'=>'昵称','type'=>'string'),
			array('name'=>'wxgid','title'=>'所属公众号','type'=>'select','options'=>'wechats'),
			array('title'=>'关联会员','type'=>'img_string','img'=>'avatar',
			'data'=>array(
				array('title'=>'账号(ID)：','name'=>'mid'),
				array('title'=>'昵称：','name'=>'mnickname')
				)
			),
			array('name'=>'follow','title'=>'是否关注','type'=>'radio','options'=>'follow_xuanxiang'),
			array('name'=>'updatetime','title'=>'关注/取消关注时间','type'=>'time'),
			array('name'=>'tags','title'=>'标签','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','op'=>'detail','title'=>'详情'),
			array('type'=>'href','op'=>'mass','title'=>'发送消息'),
			array('type'=>'modal','op'=>'tag','title'=>'添加标签'),
			array('type'=>'modal','op'=>'cancel_tag','title'=>'删除标签'),
		)),
		'search_one'=>array('name'=>'nickname','add'=>false,'select_name'=>'wxgid','select_title'=>'请选择所属公众号','options'=>'wechats'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='get'){
	$return['item']=array();
	$return['wechats']=pdo_getall('core_account_wechat',array('uniacid'=>$_SESSION['uniacid'],'type'=>array('2','4')),array('id','title'));
	$return['htmls']=array(
		'type'=>'edit',
		'post'=>'gpost',
		'desc'=>'选择公众号',
		'edit'=>array(
			array('type'=>'select','name'=>'wxgid','title'=>'公众号','options'=>'wechats')

		),
	);
	exi($return);
}
if($_RQ['op']=='gpost'){
	$postdata=get_postdata();
	$wechat=intval($postdata['wxgid']);
	if(!$wechat){
		exi('没有选择公众号！','error');
	}
	//获取粉丝信息，一次拉取一千条。先获取总数，除以1000得出一共需要几次。
	//结合每次拉取openid10000个，每次拉取粉丝信息100个。
	//获取粉丝信息时，第一步先获取所有的oepnid并入库。第二步从数据库读取openid，每次读取一千个，然后分批次拉取详细信息，并更新到数据库
	$num=cfc('account',$wechat)->openidlist();
	
	$parts=ceil($num/1000);
	$_SESSION['wechat_fans']['wechat']=$wechat;
	$_SESSION['wechat_fans']['parts']=$parts;
	exi('','','dopost');
}
if($_RQ['op']=='dopost'){
	$parts=$_SESSION['wechat_fans']['parts'];	
	$return=array();
	$return['parts']=$parts;
	exi($return);
}
if($_RQ['op']=='tongbu'){
	$wechat=$_SESSION['wechat_fans']['wechat'];
	cfc('account',$wechat)->getfanslist($_RQ['part']);
	exi('success');
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要查看的粉丝！','error');
	}
	$item=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$item['follow']=$return['follow_xuanxiang'][$item['follow']];
	$return['item']=$item;
	unset($return['follow_xuanxiang']);
	$htmls=array(
		'type'=>'look',
		'desc'=>'粉丝详情',
		'tabs'=>array(
			array('op'=>'list','title'=>'粉丝管理列表'),
		),
		'look'=>array(
			array('type'=>'image','title'=>'头像','name'=>'avatar', 'help'=>'' ),
			array('type'=>'string','title'=>'粉丝编号','name'=>'openid', 'help'=>'' ),
			array('type'=>'string','title'=>'关联会员ID','name'=>'mid','help'=>'' ),
			array('type'=>'string','title'=>'是否关注','name'=>'follow','help'=>'' ),
			array('type'=>'time','title'=>'关注/取消关注时间','name'=>'updatetime' , 'help'=>''),
			array('type'=>'string','title'=>'昵称','name'=>'nickname'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='mass'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$msgtype=intval($post['msgtype']);
		$fans=pdo_get('core_fans',array('id'=>intval($post['id']),'uniacid'=>$_SESSION['uniacid']),array('id','wxgid','openid','tagid_list'));
		if(!$fans){
			exi('指定粉丝不存在！','error');
		}
		if($msgtype){
			if(!$post['news']){
				exi('请选择文章！','error');
			}
			foreach($post['news'] as &$nv){
				$nv['url']='core/article.list/detail/id='.$nv['id'];
			}
			cfc('account')->send_news($post['news'],$fans['openid']);
		}else{
			if(!$post['content']){
				exi('请输入消息内容！','error');
			}
			cfc('account')->send_text($post['content'],$fans['openid']);
		}
		exi('发送消息成功！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择粉丝！','error');
	}
	$item=pdo_get('core_fans',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','wxgid','nickname'));
	$item['msgtype']='0';
	$return['item']=$item;
	$return['tags']=$tags;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'tip','title'=>'[<red>温馨提示</red>]：认证服务号，才可以发送消息，且该粉丝必须在48小时内有交互（跟公众号有信息交互，比如触发关键词，发送客服消息等！）'),
			array('type'=>'string','name'=>'nickname','title'=>'粉丝昵称','read'=>true),
			array('type'=>'radio','name'=>'msgtype','title'=>'消息类型','options'=>'msgtype'),
			array('type'=>'choose_many','name'=>'news','title'=>'选择文章','action'=>'core/article.article/get_article/many=1','show'=>array('msgtype'=>'1')),
			array('type'=>'text_wx','tovalue'=>'pagedata.item.content','name'=>'content','title'=>'文本内容'),
		),
	);
	$return['msgtype']=array(0=>'文本',1=>'文章');
	exi($return);
}
if($_RQ['op']=='tag'){
	//设置粉丝标签
	if($_RQ['postdata']){
		$post=get_postdata();
		$tag_id=intval($post['tag']);
		$fans=pdo_get('core_fans',array('id'=>intval($post['id']),'uniacid'=>$_SESSION['uniacid']),array('id','wxgid','openid','tagid_list'));
		if(!$fans){
			exi('指定粉丝不存在！','error');
		}
		if(strexists($fans['tagid_list'],';'.$tag_id.';')){
			exi('标签已存在，请勿重复设置！','error');
		}
		cfc('account',$fans['wxgid'])->use_tag($fans['openid'],$tag_id);
		if($fans['tagid_list']){
			$tagid_list=$fans['tagid_list'].$tag_id.';';
		}else{
			$tagid_list=';'.$tag_id.';';
		}
		pdo_update('core_fans',array('tagid_list'=>$tagid_list),array('id'=>$fans['id']));
		exi('设置标签成功！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择粉丝！','error');
	}
	$item=pdo_get('core_fans',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','wxgid','nickname'));
	//整理标签
	$tags=pdo_getall('core_fansgroup',array('uniacid'=>$_SESSION['uniacid'],'wxgid'=>$item['wxgid']),array('tag_id','title'));
	if(!$tags){
		exi('找不到标签，请先添加标签！','error','core/wechat.fansgroup/edit');
	}
	$return['item']=$item;
	$return['tags']=$tags;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'nickname','title'=>'粉丝昵称','read'=>true),
			array('type'=>'select','name'=>'tag','title'=>'添加标签','options'=>'tags','option_value'=>'tag_id'),
		),
	);
	exi($return);
}
if($_RQ['op']=='cancel_tag'){
	//设置粉丝标签
	if($_RQ['postdata']){
		$post=get_postdata();
		$tag_id=intval($post['tag']);
		$fans=pdo_get('core_fans',array('id'=>intval($post['id']),'uniacid'=>$_SESSION['uniacid']),array('id','wxgid','openid','tagid_list'));
		if(!$fans){
			exi('指定粉丝不存在！','error');
		}
		if(!strexists($fans['tagid_list'],';'.$tag_id.';')){
			exi('该粉丝没有指定标签，无需删除！','error');
		}
		cfc('account',$fans['wxgid'])->cancel_tag($fans['openid'],$tag_id);
		$tagid_list=str_replace(';'.$tag_id.';',';',$fans['tagid_list']);
		if($tagid_list==';'){
			$tagid_list='';
		}
		pdo_update('core_fans',array('tagid_list'=>$tagid_list),array('id'=>$fans['id']));
		exi('删除标签成功！');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择粉丝！','error');
	}
	$item=pdo_get('core_fans',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','wxgid','nickname','tagid_list'));
	if(!$item['tagid_list']){
		exi('该粉丝没有标签，无需去除！','error');
	}
	$tags=trim($item['tagid_list'],';');
	$tags=explode(';',$tags);
	//整理标签
	$tags=pdo_getall('core_fansgroup',array('uniacid'=>$_SESSION['uniacid'],'wxgid'=>$item['wxgid'],'tag_id'=>$tags),array('tag_id','title'));
	if(!$tags){
		exi('找不到标签，请先添加标签！','error','core/wechat.fansgroup/edit');
	}
	$return['item']=$item;
	$return['tags']=$tags;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'string','name'=>'nickname','title'=>'粉丝昵称','read'=>true),
			array('type'=>'select','name'=>'tag','title'=>'删除标签','options'=>'tags','option_value'=>'tag_id'),
		),
	);
	exi($return);
}
?>